package zhibi.controller;

import com.github.pagehelper.PageInfo;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.commons.exception.MessageException;
import zhibi.domain.User;
import zhibi.extra.base.BaseController;
import zhibi.frame.domain.Page;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;
import zhibi.service.UserService;
import zhibi.utils.StringUtils;
import zhibi.utils.security.SecurityUtils;

import java.util.Date;

/**
 * Created by 执笔 on 2018/3/24.
 */
@RequestMapping("user")
@Controller
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 员工列表
     *
     * @param user
     * @param page
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(User user, Page page, Model model) {
        Example example = Example.getInstance()
                .addParam("name", user.getName(), ExampleType.Operation.LIKE)
                .addParam("username", user.getUsername(), ExampleType.Operation.LIKE)
                .addParam("phone", user.getPhone(), ExampleType.Operation.LIKE)
                .addParam("type", "admin", ExampleType.Operation.NOTEQ)
                .addOrder("addtime", ExampleType.OrderType.DESC);
        PageInfo<User> pageInfo = userService.selectByExample(example, page);
        setModelAttribute(model, pageInfo);
        return "user/list";
    }

    @GetMapping("add")
    public String add() {
        return "user/add";
    }

    @PostMapping("add")
    public String add(User user) {
        checkAdmin();
        User temp = new User();
        temp.setUsername(user.getUsername());
        int size = userService.select(temp).size();
        if (size > 0) throw new MessageException("该员工编号已经存在");
        user.setAddtime(new Date());
        user.setPassword(SecurityUtils.MD5(user.getPassword()));
        user.setType("user");
        userService.insertSelective(user);
        return redirect("list");
    }

    @GetMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        User user = userService.selectByPrimaryKey(id);
        if (null == user) throw new MessageException("该员工不存在");
        model.addAttribute(user);
        return "user/detail";
    }

    @PostMapping("update")
    public String update(User user) {
        if (!StringUtils.isEmpty(user.getPassword()))
            user.setPassword(SecurityUtils.MD5(user.getPassword()));
        user.setUsername(null);
        userService.updateByPrimaryKeySelective(user);
        return redirect("list");
    }


}
